/**
 *author:yhb
 */
package com.towker.frame.controller.sys;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.towker.core.encrypt.EncryptUtil;
import com.towker.core.log.BaseException;
import com.towker.core.util.StrUtil;
import com.towker.frame.model.sys.User;
import com.towker.frame.service.LoginService;
import com.towker.frame.service.sys.log.TSysLogService;
import com.towker.frame.share.sys.user.UserShare;
import com.towker.web.MyRequest;


@Controller
public class LoginController
{
	protected Log log = LogFactory.getLog(getClass());
	private LoginService service;
	public void setLoginService(LoginService service){this.service = service;}
	
	private TSysLogService tSysLogService;
	//业务对象注入
	public void setTSysLogService(TSysLogService tSysLogService)
	{
		this.tSysLogService = tSysLogService;
	}
	

	/**
	 * 用户登录系统的入口。
	 * @throws IOException
	 */
	@RequestMapping("/login{viewName}")
	public ModelAndView login(HttpServletResponse response, HttpServletRequest request, @PathVariable
	String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			result=new ModelAndView();
			result.setViewName("/login.jsp");
			return result;
		}
		else if("2".equals(viewName))
		{
			MyRequest req = new MyRequest(request);
			String account = req.getString("account");
			String password = req.getString("password");
			try
			{
				User user = service.login(account, EncryptUtil.encryptMd5(password));
				if (user != null)
				{
					// 成功就跳到切换系统视图。
					UserShare.setUserLogined(request, response, user);
					String ctx = request.getContextPath();
					tSysLogService.addLoginLog(account);
//					Map map = new HashMap();
//					if(user.getUsertype()==1||user.getUsertype()==4||user.getUsertype()==5)
//					{
//						Committer committer = committerService.getById(user.getOrganid());
//						if("0".equals(committer.getRemodifiedinfo()))
//						{
//							result=new ModelAndView();
//							result.addObject("po", committer);
//							result.setViewName("/manage/zx/tiAn/redo/updCommitter.jsp");
//						}
//						else
//						{
//							response.sendRedirect(ctx + "/manage/rbac/sys/getSysByUserid.htm");
//						}
//					}
//					else if(user.getUsertype()==2||user.getUsertype()==3)
//					{
//						Organizer organizer = organizerService.getById(user.getOrganid());
//						if("0".equals(organizer.getRemodifiedinfo()))
//						{
//							map.put("pid", 0);
//							List<Organizer> organ = organizerService.getList(map);
//							result=new ModelAndView();
//							result.addObject("po", organizer);
//							result.addObject("organizer", organ);
//							result.setViewName("/manage/zx/tiAn/redo/updOrganizer.jsp");
//						}
//						else
//						{
//							response.sendRedirect(ctx + "/manage/rbac/sys/getSysByUserid.htm");
//						}
//					}
					response.sendRedirect(ctx + "/manage/rbac/sys/getSysByUserid.htm");
					//记录日志。某某人在某某时间登录了系统(干了什么事情)。日志类型。
//					BaseException ex=new BaseException(0,account,"登录系统","登录系统");
//					log.info(ex);
					return null;
				}
				else
				{
					// 失败则转回来。
					result=new ModelAndView();
					result.addObject("errorMsg", "用户名或密码错误！");
			 		result.setViewName("/login.jsp");
					//记录日志。
			 		log.error(account +",登录失败");
			 		tSysLogService.addLoginLog(account);
					return result;
				}
			}
			catch (Exception e)
			{
				log.error("login:"+e.getMessage());
			}
		}
		return result;
	}

	/**
	 * 用户登出系统。
	 */
	@RequestMapping("/logout")
	public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		tSysLogService.addOutLoginLog(UserShare.getCurrentUser(request).getAccount());
		UserShare.logout(request, response);
		response.sendRedirect(request.getContextPath() + "/login1.htm");
	}
	
	
	/**
	 * 获得验证码随机数
	 */
	@RequestMapping("/getRand")
	public void getRand(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		Random random = new Random();
		String sRand="";
		for (int i = 0; i < 4; i++)
		{
			String rand = String.valueOf(random.nextInt(10));
			sRand += rand;
		}
		PrintWriter out=response.getWriter();
		out.print(sRand);
	}
}
